package com.jvyou.house.service.impl;

import com.jvyou.common.utils.DateUtils;
import com.jvyou.house.domain.House;
import com.jvyou.house.domain.HouseCollect;
import com.jvyou.house.mapper.HouseCollectMapper;
import com.jvyou.house.mapper.HouseMapper;
import com.jvyou.house.service.IHouseCollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 橘柚
 * @since 2024/9/17 10:36
 */
@Service
public class HouseCollectService implements IHouseCollectService {

    @Autowired
    private HouseCollectMapper houseCollectMapper;

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public List<HouseCollect> selectHouseCollectList(HouseCollect houseCollect) {
        return houseCollectMapper.selectHouseCollectList(houseCollect);
    }

    @Override
    public int insertHouseCollect(HouseCollect houseCollect) {
        houseCollect.setCollectTime(DateUtils.getNowDate());
        return houseCollectMapper.insertHouseCollect(houseCollect);
    }

    @Override
    public int deleteHouseCollect(HouseCollect houseCollect) {
        return houseCollectMapper.deleteHouseCollect(houseCollect);
    }

    @Override
    public List<House> myCollect(Long id) {
        HouseCollect houseCollect = new HouseCollect();
        houseCollect.setUserId(id);
        List<HouseCollect> houseCollects = houseCollectMapper.selectHouseCollectList(houseCollect);
        List<Long> houseIds = houseCollects.stream().map(HouseCollect::getHouseId).collect(Collectors.toList());
        return houseIds.isEmpty() ? Collections.emptyList() : houseMapper.selectHouseByIds(houseIds);
    }
}
